package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class TANXINZHUAttr implements AdapterAttr {


    public static void main(String[] args) {
//        Map<String, Object> params1 = new TreeMap<>();
//        Map<String, Object> head = new HashMap<>();
//        String game = "暗区突围";
//        String url = "http://www.tanxinzhu.com/openApi?method=getExtConfig&gameName="+game+"&appid=1aa9db682e074fd5b73710e31c92bcc3";
//        String response = HttpUtils.doPostForm(url, params1, head);
//        System.out.println(response);
        String body = HttpRequest.post("http://www.tanxinzhu.com/openApi?method=games").execute().body();
        String string = JSONObject.parseObject(body).getString("data");
        List<String> jsonObjects = JSONObject.parseArray(string, String.class);
        System.out.println(body);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String body = HttpRequest.post("http://www.tanxinzhu.com/openApi?method=games").execute().body();
        String string = JSONObject.parseObject(body).getString("data");
        List<String> jsonObjects = JSONObject.parseArray(string, String.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (String jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.trim();
            String title = jsonObject.trim();
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        InterServerRes interServerRes = new InterServerRes();
        interServerRes.setId("不匹配区服");
        interServerRes.setName("不匹配区服");
        List<InterServerRes> list = new LinkedList<>();
        list.add(interServerRes);
        return list;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        String url = "http://www.tanxinzhu.com/openApi?method=getExtConfig&gameName="+gameId+"&appid=1aa9db682e074fd5b73710e31c92bcc3";
        String response = HttpUtils.doPostForm(url, params1, head);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("inspect").equals("single")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("inspect").equals("mult")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("inspect").equals("int")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {


            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setUuid(UUIDGenerator.generateUuid6());
            interAttrRes.setId(object.getString("ename"));
            interAttrRes.setName(object.getString("cname"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            String option = object.getString("option");
            if(StringUtils.isNotEmpty(option)) {
                option = option.replace("[","").replace("]","");
                String[] split = option.split(",");
                for (String o : split) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                    interAttrRes1.setId(o);
                    interAttrRes1.setName(o);
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}